package bill.study.concurrency.patterns.producerconsumer.javacip.ch5;

import java.io.File;
import java.io.FileFilter;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class FileCrawler implements Runnable{
	private final BlockingQueue<File> fileQueue;
	private final FileFilter fileFilter;
	private final File root;

	public FileCrawler(File root, FileFilter fileFilter, BlockingQueue queue) {
		this.root = root;
		this.fileFilter = fileFilter;
		this.fileQueue = queue;
	}

	public void run() {
		try {
			crawl(root);
		} catch (InterruptedException e) {
			// TODO: handle exception
		}
	}

	private void crawl(File root) throws InterruptedException {
		File[] entries = root.listFiles(fileFilter);
		if (entries != null) {
			for (File entry : entries) {
				if (entry.isDirectory()) {
					crawl(entry);
				} else if (!alreadyIndexed(entry)) {
					fileQueue.put(entry);
					System.out.println(Thread.currentThread().getName()+ ": got file: " + entry);
				}
			}
		}
	}

	private boolean alreadyIndexed(File file) {
		return false;
	}
}
